package cn.tedu.API;
/**
 * @Author 金帮嘉诚  @Date 2021-06-16 周三 20:01
 */
/*本类用于测试字符串拼接速度*/

/**
 知识点：
 StringBuilder和StringBuffer的append()方法
 优化1：创建可变字符序列对象StringBuilder和StringBuffer
 优化2：使用append()方法
 */
public class TestString {
    public static void main(String[] args) {
        String s1="abcdefghijklmnopqrstuvwxyz";
        method1(s1);
        method2(s1);
    }

    private static void method2(String s1) {
        StringBuilder sb1=new StringBuilder("");
        StringBuffer sb2=new StringBuffer("");
        long t1=System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            sb1.append(s1);
        }
        long t2=System.currentTimeMillis();
        System.out.println("sb1 = " + sb1);
        System.out.println("t2-t1 = " + (t2-t1));
    }

    private static void method1(String s1) {
        String result="";
        long t1=System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            result=result+s1;
        }
        long t2=System.currentTimeMillis();
        System.out.println("result = " + result);
        System.out.println("t2-t1 = " + (t2-t1));
    }

}
